﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Estsh.Core.Controllers;

namespace Estsh.Core.Web.Areas.Wms.Controllers.CycleCount
{
    [Route("wms/[controller]")]
    [ApiController]
    public class CycleCountController : WmsBaseController
    {
        private ICycleCountService service;

        // GET: LoginController
        public CycleCountController(ICycleCountService _service)
        {
            service = _service;
        }

        [HttpPost("GetCycleCountOrderList")]
        public IActionResult GetCycleCountOrderList(Newtonsoft.Json.Linq.JObject jobj)
        {
            string orderNo = jobj["orderNo"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetCycleCountOrderList(orderNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("GetCycleCountOrderListByOrderNo")]
        public IActionResult GetCycleCountOrderListByOrderNo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string orderNo = jobj["orderNo"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetCycleCountOrderListByOrderNo(orderNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("GetLocateByName")]
        public IActionResult GetLocateByName(Newtonsoft.Json.Linq.JObject jobj)
        {
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetLocateByName(locateName, orderNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        // 扫描箱条码
        [HttpPost("CheckStockByCartonAlocate")]
        public IActionResult CheckStockByCartonAlocate(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockByCartonAlocate(cartonNo, locateName, orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        [HttpPost("CheckStockByCartonListAlocate")]
        public IActionResult CheckStockByCartonListAlocate(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockByCartonListAlocate(cartonNo, locateName, orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        /// <summary>
        /// 点击周转箱来判断数据
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckStockByCartonBox")]
        public IActionResult CheckStockByCartonBox(Newtonsoft.Json.Linq.JObject jobj)
        {
            string turnoverBoxNo = jobj["box"].ToString().Trim();
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();
            string qty = jobj["qty"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockByCartonBox( turnoverBoxNo,  cartonNo,  locateName,  orderNo,  loginId, qty);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        // 扫描箱条码，库位不一致
        [HttpPost("CheckStockFactQtyByLocatePrompt")]
        public IActionResult CheckStockFactQtyByLocatePrompt(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockFactQtyByLocatePrompt(cartonNo, locateName, orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        //扫描箱条码 新增
        [HttpPost("CheckStockFactQtyByAddPrompt")]
        public IActionResult CheckStockFactQtyByAddPrompt(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockFactQtyByAddPrompt(cartonNo, locateName, orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        //确认输入数量完成盘点
        [HttpPost("CheckStockByCartonAFactQty")]
        public IActionResult CheckStockByCartonAFactQty(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string locateName = jobj["locateName"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string qty = jobj["qty"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckStockByCartonAFactQty(cartonNo, locateName, orderNo, qty, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
    }
}
